# -*- coding: utf-8 -*-
'''
Created on Sep 20, 2013

@author: LONG HOANG GIANG
'''
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import lib
import logging
import json

def convert(path):
    if not path.endswith("/") and not path.endswith("\\"):
        path += "\\"
    path += "data"
    print path
    if not os.path.exists(path):
        raise
    data = lib.gz_file_get_content(path)
    try:
        raw = lib.decryptCipher(data)
    except:
        raw = data
    print raw
    data = json.loads(raw)
    newdata = []
    try:
        for item in data:
            newdata.append({'name': item['title'], 'path': item['id']})
        edata = lib.encryptCipher(json.dumps(newdata))
        os.unlink(path)
        lib.gz_file_put_content(edata, path)
    except:
        logging.warning("This is correct data file, not need to convert now")

if __name__ == '__main__':
    
    path = raw_input("Nhap duong dan thu muc chua file data: ")
    convert(path)
    
    logging.info("finished")
    os._exit(1)